using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Drone:Mob {

	[SerializeField] float damage = 1;
	[SerializeField] AnimationSet spritesAttack;
	[SerializeField] AnimationSet spritesAttackHurt;

	protected override void UpdateTimer() {
		base.UpdateTimer();
		timeAfterDamage+=deltaTime;
	}

	float timeAfterDamage = 0;
	const float damageDelay = 0.5f;

	protected override void CreateDamageParticle(Vector2 position) {
		Particles.CreateParticle("ParticleSpark",position);
	}

	Vector2 chargeDirection;
	const float chargeTime = 1f;
	override protected void StateAttack() {
		if(!target) {
			GoIdle();
			return;
		}
		if(IfLineOfSight(target)) targetPosition=target.position;
		chargeDirection=targetPosition-rigidbody.position;
		targetVelocity=chargeDirection.normalized*speed*1.5f;
		if(stateTime>chargeTime) {
			idleTime=0.5f;
			ChangeState(StateIdle);
		}

		//����˺�
		if(collider.bounds.Intersects(target.collider.bounds)) {
			if(timeAfterDamage>damageDelay) {
				//ȷ������˺�	
				target.Damage(damageStats);
			}
		}
	}

	DamageStats damageStats {
		get {
			timeAfterDamage=0;
			Vector2 damagePosition = collider.ClosestPoint(target.position);
			Vector2 knockback = (target.rigidbody.position-rigidbody.position).normalized*1.5f;
			rigidbody.velocity-=knockback;

			return new DamageStats(damage,this,null,null,damagePosition,knockback);
		}
	}
}